<template>
  <div
    class="sidebar-container"
    :class="[
      `sidebar-${property.position}`,
      { 'sidebar-shadow': property.style.shadow }
    ]"
    :style="{
      top: property.top + 'px',
      backgroundColor: property.style.sidebarBgColor,
      borderRadius: property.style.sidebarBorderRadius + 'px'
    }"
  >
    <div class="sidebar-items">
      <div
        v-for="item in enabledItems"
        :key="item.id"
        class="sidebar-item"
        :style="{
          backgroundColor: item.backgroundColor,
          borderRadius: property.style.sidebarBorderRadius + 'px'
        }"
        @click="openDialog(item)"
      >
        <img
          :src="item.iconUrl"
          :alt="item.title"
          class="sidebar-icon"
          @error="handleImageError"
        />
      </div>
    </div>

    <!-- 弹框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="currentItem?.title"
      width="80%"
      :show-close="true"
      :close-on-click-modal="true"
      :close-on-press-escape="true"
      class="sidebar-dialog"
    >
      <div
        class="dialog-content"
        v-html="formatContent(currentItem?.content || '')"
      ></div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { SidebarProperty, SidebarItem } from './config'
import { ref, computed } from 'vue'

defineOptions({ name: 'Sidebar' })

interface Props {
  property: SidebarProperty
}

const props = defineProps<Props>()

const dialogVisible = ref(false)
const currentItem = ref<SidebarItem | null>(null)

const enabledItems = computed(() => {
  if (!props.property.items || !Array.isArray(props.property.items)) {
    return []
  }
  return props.property.items.filter(item => item && item.enabled === true)
})

const openDialog = (item: SidebarItem) => {
  if (!item) return
  currentItem.value = { ...item }
  dialogVisible.value = true
}


const formatContent = (content: string) => {
  if (!content) return ''
  return content.replace(/\n/g, '<br>')
}

const handleImageError = (event: Event) => {
  const img = event.target as HTMLImageElement
  img.src = ''
}
</script>

<style scoped lang="scss">
.sidebar-container {
  position: fixed;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
  transition: all 0.3s ease;

  &.sidebar-left {
    left: 10px;
  }

  &.sidebar-right {
    right: 10px;
  }

  &.sidebar-shadow {
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  }
}

.sidebar-items {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-item {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);

  &:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }

  &:active {
    transform: scale(0.95);
  }
}

.sidebar-icon {
  width: 24px;
  height: 24px;
  object-fit: contain;
  filter: brightness(0) invert(1);
}

.dialog-content {
  line-height: 1.6;
  color: #333;
  font-size: 14px;
  white-space: pre-wrap;
}

// 弹框样式优化
:deep(.sidebar-dialog) {
  .el-dialog__header {
    padding: 16px 20px 8px;
    border-bottom: 1px solid #f0f0f0;
  }

  .el-dialog__title {
    font-size: 16px;
    font-weight: 600;
    color: #333;
  }

  .el-dialog__body {
    padding: 16px 20px 20px;
  }

  .el-dialog__close {
    color: #999;
    font-size: 18px;

    &:hover {
      color: #666;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .sidebar-container {
    padding: 6px;

    &.sidebar-left {
      left: 8px;
    }

    &.sidebar-right {
      right: 8px;
    }
  }

  .sidebar-item {
    width: 44px;
    height: 44px;
  }

  .sidebar-icon {
    width: 22px;
    height: 22px;
  }
}

@media (max-width: 480px) {
  .sidebar-container {
    padding: 4px;

    &.sidebar-left {
      left: 6px;
    }

    &.sidebar-right {
      right: 6px;
    }
  }

  .sidebar-item {
    width: 40px;
    height: 40px;
  }

  .sidebar-icon {
    width: 20px;
    height: 20px;
  }
}
</style>
